﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;

namespace WindowsFormsApplicationCSII
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
            toolStripStatusLabelCount.Text = "Count: 0";
        }

        private char[] delimaters = { ' ', ',', '.', '\t', '\n', ':', '!', ';' };

        private int GetWordsCount(string text)
        {
            var words = text.Split(delimaters, StringSplitOptions.RemoveEmptyEntries);
            return words.Length;
        }

        private void textBoxEditor_TextChanged(object sender, EventArgs e)
        {
            //Debug.WriteLine(textBoxEditor.Text.Length);
            string countText = String.Format("Count: {0}", GetWordsCount(textBoxEditor.Text));
            toolStripStatusLabelCount.Text = countText;
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowSaveDialog();
            
            //string folder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            //string fileName = "data.txt";
            //string path = Path.Combine(folder, fileName);
            //File.WriteAllText(path, textBoxEditor.Text);
        }

        private void ShowSaveDialog()
        {
            if (saveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                File.WriteAllText(saveFileDialog.FileName, textBoxEditor.Text);
            }
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                textBoxEditor.Text = File.ReadAllText(openFileDialog.FileName);
            }
            
            //string folder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            //string fileName = "data.txt";
            //string path = Path.Combine(folder, fileName);
            //textBoxEditor.Text = File.ReadAllText(path);
        }

        private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            var result = MessageBox.Show(
                "Do you want to save before closing?", 
                "Warning", 
                MessageBoxButtons.YesNo,
                MessageBoxIcon.Warning
                );
            if (result == System.Windows.Forms.DialogResult.Yes)
            {
                ShowSaveDialog();
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        
    }
}
